package pack4_builder;

import java.util.ArrayList;
import java.util.List;

//车模
abstract class CarModel{

    //通过sequence可以组装车辆的行驶行为
    private List<String> sqeuence = new ArrayList<String>();

    public abstract void start();
    public abstract void stop();
    public abstract void alarm();
    public abstract void brake();

    //不允许被子类重写, 在执行run时会调用对象（this）的重写方法
    public final void run(){
        for (String action : sqeuence) {
            if(action.equals("start")){
                this.start();            
            }else if(action.equals("stop")){
                this.stop();
            }else if(action.equals("alarm")){
                this.alarm();
            }else{
                this.brake();
            }
        }
    }

    //不允许被子类重写, sequence中封装着车模一系列的行为
    public void setSequence(List<String> sequenceList){
        this.sqeuence = sequenceList;
    }
}

class Benz extends CarModel{

    @Override
    public void start() {
        System.out.println("Benz start");
    }

    @Override
    public void stop() {
        System.out.println("Benz stop");
    }

    @Override
    public void alarm() {
        System.out.println("Benz alarm");
    }

    @Override
    public void brake() {
        System.out.println("Benz brake");
    }
}

class BMW extends CarModel{

    @Override
    public void start() {
        System.out.println("BMW start");
    }

    @Override
    public void stop() {
        System.out.println("BMW stop");
    }

    @Override
    public void alarm() {
        System.out.println("BMW alarm");
    }

    @Override
    public void brake() {
        System.out.println("BMW brake");
    }
}

//抽象生成器
abstract class CarBuilder{
    abstract CarModel getCarModel();
    abstract void setSequence(CarModel carModel, List<String> sequence);
}

//生产器子类：每个产品使用一个生成器（Builder）来生产
class BenzBuilder extends CarBuilder{

    @Override
    public CarModel getCarModel() {
        CarModel carModel = new Benz();
        return carModel;
    }

    @Override
    public void setSequence(CarModel carModel, List<String> sequence) {
        carModel.setSequence(sequence);
    }
}

class BMWBuilder extends CarBuilder{

    @Override
    public CarModel getCarModel() {
        CarModel carModel = new BMW();
        return carModel;
    }

    @Override
    public void setSequence(CarModel carModel, List<String> sequence) {
        carModel.setSequence(sequence);
    }
}

//指导类：用于对各个生成器进行进一步封装
class Director{
    private CarBuilder BMW_builder = new BMWBuilder();
    private CarBuilder Benz_builder = new BenzBuilder();

    public CarModel getBMW_Model(){
        CarModel carModel = BMW_builder.getCarModel();
        List<String> sequence = new ArrayList<>();
        sequence.add("start");
        sequence.add("alarm");
        sequence.add("brake");
        sequence.add("stop");
        BMW_builder.setSequence(carModel, sequence);
        return carModel;
    }

    public CarModel getBenz_Model(){
        CarModel carModel = Benz_builder.getCarModel();
        List<String> sequence = new ArrayList<>();
        sequence.add("start");
        sequence.add("brake");
        sequence.add("alarm");
        sequence.add("stop");
        BMW_builder.setSequence(carModel, sequence);
        return carModel;
    }
}

public class code001_builder {
    public static void main(String[] args) {
        Director director = new Director();
        CarModel BMW = director.getBMW_Model();
        CarModel Benz = director.getBenz_Model();
        BMW.run();
        Benz.run();
    }
}
